package net.lzzy.practices.services;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.ChapterRepository;
import net.lzzy.practices.repositories.CourseRepository;
import net.lzzy.practices.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ChapterServiceImpl implements ChapterService{
    private final ChapterRepository repository;

    private final CourseRepository cRepository;

    public ChapterServiceImpl(ChapterRepository repository, CourseRepository cRepository){
        this.repository = repository;
        this.cRepository = cRepository;
    }

    @Override
    public List<Chapter> getTeachersChapters(UUID courseId, String token) {
        if (checkTeacher(courseId,token) == null){
            return null;
        }
        return repository.findChaptersByCourseIdOrderByUpdateTimeDesc(courseId);
    }

    private Course checkTeacher(UUID courseId,String token){
        Course course = cRepository.findById(courseId).orElse(null);
        if (course == null){
            return null;
        }
        UUID teacherId = course.getTeacher().getId();
        if (AuthUtils.invalid(teacherId, token)){
            return null;
        }
        return course;
    }
    @Override
    public List<Chapter> getStudentsChapters(UUID courseId, String token) {
        Course course = cRepository.findById(courseId).orElse(null);
        if (course == null){
            return null;
        }
        List<User> students = course.getStudents();
        UUID studentId = AuthUtils.getUser(token).getId();
        long count = students.stream().filter(s -> s.getId().equals(studentId)).count();
        if (count == 0){
            return null;
        }
        return repository.findChaptersByCourseIdAndOpenOrderByUpdateTimeDesc(courseId,true);
    }

    @Override
    public Chapter add(UUID courseId, Chapter chapter, String token) {
        Course course = checkTeacher(courseId,token);
        if (course == null){
            return null;
        }
        chapter.setCourse(course);
        return repository.save(chapter);
    }

    @Override
    public Chapter update(UUID id, Chapter chapter, String token) {
        Chapter old = repository.findById(id).orElse(null);
        if (old == null){
            return null;
        }
        UUID teacherId = old.getCourse().getTeacher().getId();
        if (AuthUtils.invalid(teacherId,token)){
            return null;
        }
        old.setDescription(chapter.getDescription());
        old.setName(chapter.getName());
        old.setOpen(chapter.isOpen());
        old.setUpdateTime(new Date());
        return repository.save(old);
    }

    @Override
    public Chapter openCloseChapter(UUID id, boolean open, String token) {
        Chapter old = repository.findById(id).orElse(null);
        if(old != null){
            old.setOpen(open);
        }
        return update(id,old,token);
    }

    @Override
    public Messages remove(UUID id, String token) {
        Chapter chapter = repository.findById(id).orElse(null);
        if (chapter == null){
            return Messages.WRONG_ID;
        }
        UUID teacherId = chapter.getTeacher().getTeacher().getId();
        if (AuthUtils.invalid(teacherId,token)){
            return Messages.NO_PERMISSION;
        }
        if (chapter.getQuestions().size() > 0){
            return Messages.RELATED_DATA;
        }
        repository.delete(chapter);
        return Messages.SUCCESS;
    }
}
